{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import sys, os\n",
    "sys.path.append(\"C:\\\\Users\\\\Administrator\\\\Desktop\\\\Deeptrain\")\n",
    "os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Deeptrain.settings')\n",
    "os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rest.settings')\n",
    "os.environ[\"DJANGO_ALLOW_ASYNC_UNSAFE\"] = \"true\"\n",
    "\n",
    "import django\n",
    "django.setup()\n",
    "\n",
    "from utils.benchmark import *\n",
    "from utils.cache import hash_cache_process_safe as hash_cache\n",
    "from utils.cache import integer_operation\n",
    "from utils.throttle import rate_throttle\n",
    "from user.models import User\n",
    "from blog.models import Article\n",
    "def req():\n",
    "    return Article.objects.filter(author=User.objects.get()).count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "[350.36589030132376,\n 566.5262114867088,\n 558.8596146200083,\n 581.2723842594422,\n 577.5986198128683,\n 575.3486066297194,\n 572.461102695272,\n 549.1960308339305,\n 562.9722134156997,\n 587.5412187584924,\n 604.876619304924,\n 577.7997701091156,\n 419.43723682696026,\n 358.59135961763974,\n 567.3856954225412]"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[fps_analysis(req, count=1000) for _ in range(15)]  # 预热cache"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "583.05012366328"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fps_analysis(req, count=1000)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "'function fps: 65351.076874656246, compared function fps: 566.0337763798809'"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fps_compare(hash_cache(req), req, count=10**4)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "integer_operation(\"some-cache\", expiration=1)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "2"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "integer_operation(\"asdf\", expiration=1)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "integer_operation(\"asdf\", expiration=1)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "5"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "integer_operation(\"asdf\", expiration=10)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "[False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n False,\n True]"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[rate_throttle(\"somekey\", 99) for _ in range(100)]"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False, False, False, False, False, False, False, False, False, True]\n",
      "[False, False, False, False, False, False, False, False, False, True]\n",
      "[False, False, False, False, False, False, False, False, False, True]\n",
      "[False, False, False, False, False, False, False, False, False, True]\n",
      "[False, False, False, False, False, False, False, False, False, True]\n",
      "[False, False, False, False, False, False, False, False, False, True]\n",
      "[False, False, False, False, False, False, False, False, False, True]\n",
      "[False, False, False, False, False, False, False, False, False, True]\n",
      "[False, False, False, False, False, False, False, False, False, True]\n",
      "[False, False, False, False, False, False, False, False, False, True]\n"
     ]
    }
   ],
   "source": [
    "from time import sleep\n",
    "\n",
    "for _ in range(10):\n",
    "    sleep(1)\n",
    "    print([rate_throttle(\"val\", 9, expiration=1, version=\"version-2\") for _ in range(10)])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False, False, False, False, False, False, False, False, False, True, True, True]\n",
      "[True, True, True, True, True, True, True, True, True, True, True, True]\n",
      "[True, True, True, True, True, True, True, True, True, True, True, True]\n",
      "[False, False, False, False, False, False, False, False, False, True, True, True]\n",
      "[False, False, False, False, False, False, False, False, False, True, True, True]\n",
      "[True, True, True, True, True, True, True, True, True, True, True, True]\n"
     ]
    }
   ],
   "source": [
    "for _ in range(6):\n",
    "    sleep(1)\n",
    "    print([rate_throttle(\"val\", 9, expiration=1.01, version=\"version-1\", touch=True) for _ in range(12)])"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
